package cn.itsource.itsource.service.impl;

import cn.itsource.itsource.domain.SearchMasterMessage;
import cn.itsource.itsource.domain.Shop;
import cn.itsource.itsource.domain.User;
import cn.itsource.itsource.mapper.SearchMasterMessageMapper;
import cn.itsource.itsource.mapper.ShopMapper;
import cn.itsource.itsource.query.SearchMasterMessageQuery;
import cn.itsource.itsource.service.ISearchMasterMessageService;
import cn.itsource.itsource.service.RedisService;
import cn.itsource.itsource.util.AjaxResult;
import cn.itsource.itsource.util.DistanceUtil;
import cn.itsource.itsource.util.PageList;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jdk.internal.org.objectweb.asm.tree.TryCatchBlockNode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Distance;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class SearchMasterMessageServiceImpl implements ISearchMasterMessageService {

    @Autowired
    private SearchMasterMessageMapper searchMasterMessageMapper;
    @Autowired
    private RedisService redisService;
    @Autowired
    private ShopMapper shopMapper;


    @Override
    public AjaxResult publish(SearchMasterMessage searchMasterMessage, String token) {
        //完成最简单的业务

        //设置部分字段的默认值(user_id)
        //user_id是根据token令牌从redis中取出用户的信息
        User user = redisService.getKeyObjectValue(token, User.class);
        if (user == null) {
            return AjaxResult.error("当前用户未登录");
        }
        searchMasterMessage.setUserId(user.getId());
        searchMasterMessage.setCreateTime(new Date());
        //shop_id先预留:根据当前用户发布寻主信息填写的地址，与店铺的地址进行距离计算，找到最近门店
        Shop shop = calMinDistanceToShop(searchMasterMessage.getAddress());
        searchMasterMessage.setShopId(shop.getId());
        searchMasterMessageMapper.insertSelective(searchMasterMessage);

        return AjaxResult.success("发布成功");

    }

    @Override
    public PageList<SearchMasterMessage> findPage(SearchMasterMessageQuery query) {
        Example example = new Example(SearchMasterMessage.class);
        if (StringUtils.isNotBlank(query.getKeyword())) {
            example.and().andLike("title", "%" + query.getKeyword() + "%").orLike("address", "%" + query.getKeyword() + "%");
        }
        //添加分页效果
        PageHelper.startPage(query.getCurrPage(), query.getPageSize());
        PageInfo<SearchMasterMessage> pageInfo = new PageInfo<>(searchMasterMessageMapper.selectByExample(example));

        return new PageList<>(pageInfo.getTotal(), pageInfo.getList());

    }

    private Shop calMinDistanceToShop(String petAddress){
        List<Shop> shopList=shopMapper.selectAll();
        //假设第一个店铺是最小距离店铺
        Shop minDistanceShop = shopList.get(0);
        double minDistance = DistanceUtil.calTwoAddressOfDistance(petAddress, minDistanceShop.getAddress());
        for (Shop shop:shopList){
            try{
                Double distance=DistanceUtil.calTwoAddressOfDistance(petAddress,shop.getAddress());
                if (distance<minDistance){
                    minDistance=distance;
                    minDistanceShop=shop;
                }

            }catch (Exception e){



            }

        }
        return minDistanceShop;



    }
}
